package com.rlyy.basic.resolver.exception;

import com.rlyy.common.exception.ParameterParseException;
import com.rlyy.common.exception.ParameterValidException;
import com.rlyy.common.response.Result;
import com.rlyy.common.response.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 参数解析、校验统一异常处理器
 * 实现接口PriorityOrdered默认排序优先级高
 * 如果业务系统想替换此异常处理器，使用@Primary注解
 *
 * @author yuanqinglong
 * @since 2020/11/30 9:46
 */
@ControllerAdvice
public class ParameterExceptionHandler implements PriorityOrdered {

    private static final Logger LOG = LoggerFactory.getLogger(ParameterExceptionHandler.class);

    /**
     * 参数解析异常
     *
     * @param exception 异常信息
     * @return 封装错误返回数据
     */
    @ExceptionHandler(ParameterParseException.class)
    public Result parameterParseException(ParameterParseException exception) {
        LOG.error("参数解析异常异常信息：", exception);
        return ResultUtil.error(exception);
    }

    /**
     * 参数校验异常
     *
     * @param exception 异常信息
     * @return 封装错误返回数据
     */
    @ExceptionHandler(ParameterValidException.class)
    public Result parameterValidException(ParameterValidException exception) {
        LOG.warn("参数校验异常异常信息：{}", exception.getMessage());
        return ResultUtil.error(exception);
    }



    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}
